package com.training.food.service.Impl;

import com.training.food.Domain.Confirm;
import com.training.food.Domain.Confirmslauger;
import com.training.food.Domain.HisOrder;
import com.training.food.Domain.Release;
import com.training.food.Mapper.InitialpurMapper;
import com.training.food.Mapper.RetailMapper;
import com.training.food.Mapper.SlaughterMapper;
import com.training.food.Mapper.WSmarketMapper;
import com.training.food.pojo.Initialpur;
import com.training.food.pojo.User;
import com.training.food.service.ConfirmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ConfirmServiceImpl implements ConfirmService {

    @Autowired
    SlaughterMapper slaughterMapper;

    @Autowired
    WSmarketMapper wSmarketMapper;

    @Autowired
    RetailMapper retailMapper;

    @Autowired
    InitialpurMapper initialpurMapper;

    @Override
    @Transactional
    public boolean confirm(Confirm confirm, char power) {
        /**
        *判断power：屠宰厂权限则操作屠宰厂减少，批发市场状态更改,填充关系表
         * 批发市场则操作批发市场减少，零售商状态更改
         */
        if(power=='1'){
            int pf=confirm.getPf();
//            处理屠宰厂被选中的批次的更新
            slaughterMapper.updateConfirm(confirm.getTz());
//            处理批发市场更新订单的状态为6，表示这个是一个订单记录并且已经买到了
            wSmarketMapper.updateState(confirm.getPf(),"6");
//            填充关系表
            for (Confirmslauger confirmslauger:confirm.getTz()
                 ) {
                Initialpur initialpur=new Initialpur(pf,confirmslauger.getPici());
                initialpurMapper.insert(initialpur);
            }
            return true;
        }
        if(power=='2'){
            wSmarketMapper.updateConfirm(confirm.getTz());
            retailMapper.updateState(confirm.getPf(),"6");
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public boolean addReleaseOrder(Release release, User user){
        if(user.getPower()=='2'){
            wSmarketMapper.addReleaseOrder(release,user,'4');
            return true;
        }
        if(user.getPower()=='3'){
            retailMapper.addReleaseOrder(release,user,'4');
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public List<HisOrder> allOrderShow(User user, List<String> states){
        if(user.getPower()=='2'){
            List<HisOrder> res=wSmarketMapper.allReleaseShow(user,states);
            return res;
        }
        if(user.getPower()=='3'){
            List<HisOrder> res=retailMapper.allReleaseShow(user,states);
            return res;
        }
        return null;
    }
}
